//
//  PopulationViewController.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/11/22.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "PopulationViewController.h"
#import "CaseHistoryModel.h"

@interface PopulationViewController ()
@property (strong, nonatomic) NSMutableArray *arrDataSource;

@end

@implementation PopulationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"人群分类";
    [self showNavgationWithLeftItemWithTitle:@"返回"];
    [self showNavBarWithRightImage:[UIImage imageNamed:@"icon_save"] andRightBtnAction:@selector(save) LeftBtnImage:nil andLeftBtnAction:nil title:nil barColor:nil];
    [self requestUserInfo];
}

#pragma mark 数据请求
- (void)requestUserInfo{
    [SVProgressHUD showWithStatus:@"数据请求.." maskType:SVProgressHUDMaskTypeClear];
    [[BaseDataRequest alloc] populationWithBlock:^(id responseObject, NSString *status, NSError *error) {
        [SVProgressHUD dismiss];
        if ([status isEqualToString:@"0"]) {
            NSMutableArray *dicResult = [responseObject objectForKey:@"result"];
            for (int i = 0 ; i < dicResult.count; i++) {
                CaseHistoryModel *model = [[CaseHistoryModel alloc] init];
                model.name = dicResult[i];
                if ([self.crowdtype isEqualToString:model.name]) {
                    model.select = YES;
                }else{
                    model.select = NO;
                }
                [self.arrDataSource addObject:model];
            }
            [self.tableView reloadData];
        }else{
            if (error != nil) {
                TIPSFORVIEW(@"请检测网络设置");
            }else{
                TIPSFORVIEW(@"获取用户信息失败");
            }
        }
    }];
}


#pragma  mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _arrDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"populationCellID" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"populationCellID"] ;
    }
    UILabel *labName = [cell.contentView viewWithTag:1];
    UIImageView *image = [cell.contentView viewWithTag:2];
    CaseHistoryModel *model = _arrDataSource[indexPath.row];
    labName.text = model.name;
    if (model.select) {
        image.hidden = NO;
    }else{
        image.hidden = YES;
    }
    return cell;
}

#pragma mark - UITableview Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    for (CaseHistoryModel *model in _arrDataSource) {
        model.select = NO;
    }
    CaseHistoryModel *model = _arrDataSource[indexPath.row];
    model.select = !model.select;
    [_arrDataSource replaceObjectAtIndex:indexPath.row withObject:model];
    [self.tableView reloadData];
}


#pragma mark - set/get
- (NSMutableArray *)arrDataSource{
    if (_arrDataSource == nil) {
        _arrDataSource = [NSMutableArray array];
    }
    return _arrDataSource;
}


#pragma mark -
- (void)save{
    NSString *strName = @"";
    for (int i = 0 ; i < _arrDataSource.count; i++) {
        CaseHistoryModel *model = _arrDataSource[i];
        if (model.select) {
            strName = model.name;
        }
    }
    if ([strName isEqualToString:@""]) {
        TIPSFORVIEW(@"请选择人群分类");
        return;
    }
    [self.deleaget backSelectClass:strName];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
